﻿using HardcoreGame.Common.Abstract;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using Terraria;
using Terraria.Localization;
using Terraria.ModLoader;
namespace HardcoreGame.Common.Cores.Seasons
{
    public class Winter : Season
    {
        private readonly static List<DecalAdditionInfo> decalsToAdd;
        private Rain[] rainPrev;
        public bool noRainParticles;

        public override string Name => Language.GetTextValue("Mods.HardcoreGame.Season.Winter");
        public override void ApplySeasonEffect()
        {
            Main.SceneMetrics.EvilTileCount = (int)(Main.SceneMetrics.EvilTileCount * 1.5f);
            Main.SceneMetrics.BloodTileCount = (int)(Main.SceneMetrics.BloodTileCount * 1.5f);
            noRainParticles = true;
            Player localPlayer = Main.LocalPlayer;
            if (localPlayer.ZoneOverworldHeight && !localPlayer.ZoneJungle && !localPlayer.ZoneDesert && !localPlayer.ZoneBeach)
            {
                int snowTiles = Main.SceneMetrics.SnowTileCount;
                Main.SceneMetrics.SnowTileCount = 800;
                try
                {
                    int num = Main.raining ? 3 : 1;
                    for (int i = 0; i < num; i++)
                    {
                        Main.snowing();
                    }
                }
                catch
                {
                }
                Main.SceneMetrics.SnowTileCount = snowTiles;
            }
            if (Main.rain != null)
            {
                bool flag = !noRainParticles && rainPrev != null && rainPrev.Length == Main.rain.Length;
                for (int i = 0; i < Main.rain.Length; i++)
                {
                    Rain rain = Main.rain[i];
                    if (rain != null)
                    {
                        if (rain.active)
                        {
                            if (noRainParticles)
                            {
                                rain.active = false;
                            }
                        }
                        else if (flag)
                        {
                            Rain rain2 = rainPrev[i];
                            if (rain2 != null && rain2.active)
                            {
                                Point point = rain.position.ToPoint();
                                ClearDecals(new Rectangle(point.X - 4, point.Y - 4, 8, 8));
                            }
                        }
                    }
                }
                noRainParticles = false;
                rainPrev = (Rain[])Main.rain.Clone();
            }
        }
        public static void ClearDecals(Rectangle rect)
        {
            AddDecals(rect, Color.Transparent, true);
        }
        public static void AddDecals(Rectangle rect, Color color, bool ifChunkExists = false)
        {
            if (Main.dedServ || WorldGen.gen || WorldGen.IsGeneratingHardMode)
            {
                return;
            }
            decalsToAdd.Add(new DecalAdditionInfo(rect, color, ifChunkExists));
        }
        private struct DecalAdditionInfo(Rectangle rect, Color color, bool ifChunkExists)
        {
            public Rectangle rect = rect;
            public Color color = color;
            public bool ifChunkExists = ifChunkExists;
        }
    }
}
